<template>
  <div></div>
</template>

<script setup lang="ts">
  import { PrismaClient } from '@prisma/client'

  const prisma = new PrismaClient()

  type User = {
    id: number
    username: string
    age: number
    userType: string
    email: string
    sort: number
  }

  type Student = {}

  // 添加数据
  async function addUser(user: Omit<User, 'id'>) {
    await prisma.user.create({ data: user })
  }

  // 删除数据
  async function delUser(id: number) {
    await prisma.user.delete({ where: { id } })
  }
  // 修改数据
  async function updateUser(user: Partial<User>) {
    await prisma.user.update({
      where: { id: user.id },
      data: user
    })
  }

  // 查询数据
  async function getUser(id: number) {
    return await prisma.user.findUnique({ where: { id } })
  }

  // 查询所有数据
  async function getUserList() {
    return await prisma.user.findMany()
  }

  // 关联查询
  async function getUserWithPosts(id: number) {
    return await prisma.user.findUnique({
      where: { id },
      // 关联查询该用户的 posts
      include: { posts: true }
    })
  }

  async function main() {
    // await addUser({ username: 'kunkun', age: 18, userType: 'user', email: 'kunkun@qq.com', sort: 10 })
    // await addUser({ username: '唔西迪西', age: 20, userType: 'admin', email: 'wuxidxi@qq.com', sort: 12 })
    // await prisma.user.create({
    //   data: {
    //     username: '玛卡巴卡',
    //     age: 25,
    //     userType: 'admin',
    //     email: 'makabaka@qq.com',
    //     sort: 14,
    //     posts: {
    //       create: [
    //         {
    //           title: '标题1',
    //           content: '内容1',
    //           published: true
    //         },
    //         {
    //           title: '标题2',
    //           content: '内容2',
    //           published: false
    //         }
    //       ]
    //     }
    //   }
    // })
    // await delUser(2)
    // await updateUser({ id: 1, username: '坤坤', userType: 'admin' })
    // let user = await getUser(1)
    // console.log(user)

    let users = await getUserList()
    console.log(users)

    let userWithPosts = await getUserWithPosts(3)
    console.log(userWithPosts)
  }
  main()
</script>

<style scoped></style>
